﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DreamBuilder.Common;

namespace Web.SystemConfig.DataAccess
{
    public class DALAuth_Role : DataAccessBase
    {
        internal DataTable GetAllMenuData()
        {
            //modify by lyn  string strSql = "SELECT * FROM P_Auth_Menu where Menu_ID!='ePlanet.Configuration.Auth_Menu' and Menu_ID!='ePlanet.Configuration.CodeTable' ORDER BY Parent_Menu_ID,Order_Num ";
            string strSql = "SELECT * FROM P_Auth_Menu ORDER BY Parent_Menu_ID,Order_Num ";
            return DBHelper.ExecuteDataSet(strSql).Tables[0];
        }

        internal DataTable GetAllFunctionData()
        {
            string strSql = "SELECT * FROM P_Auth_Function";
            return DBHelper.ExecuteDataSet(strSql).Tables[0];
        }

        internal DataTable GetFunctionByRoleID(string Role_ID)
        {
            string strSql = "SELECT * FROM P_Auth_Role_Function where Role_ID = '" + Role_ID + "'";
            return DBHelper.ExecuteDataSet(strSql).Tables[0];
        }

        internal void SaveFuncs(List<string> listFunc_ID, string roleID)
        {
            StringBuilder sb = new StringBuilder();
            if (listFunc_ID.Count > 0)
            {
                sb.Append("begin ");
                foreach (string funcID in listFunc_ID)
                {
                    string sql = " INSERT INTO P_Auth_Role_Function VALUES('" + roleID + "','" + funcID + "'); \n ";
                    sb.Append(sql);
                }
                sb.Append("end; ");
                if (!string.IsNullOrEmpty(sb.ToString()))
                {
                    DBHelper.ExecuteSql(sb.ToString());
                }

            }
        }
        internal void DeleteOldFuncData(string roleID)
        {
            string strSql = " DELETE FROM P_Auth_Role_Function where Role_ID = '" + roleID + "'";
            DBHelper.ExecuteSql(strSql);
        }

        internal bool IsThisRole(string userID, string roleID)
        {
            string strSql = string.Format("select * from p_auth_user_role where user_ID='{0}' and role_id='{1}'", userID, roleID);
            DataTable oDataTable = this.DBHelper.ExecuteDataSet(strSql).Tables[0];
            if (oDataTable.Rows.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        internal DataSet GetDeleteChildInfo(string pkValue)
        {
            DataSet dsInfo = new DataSet();
            DataTable dtInfo = null;
            string sSql = "select P_Auth_Role_Function.*,'3' as RowStatus from P_Auth_Role_Function where Role_ID = '" + pkValue + "'";
            dtInfo = DBHelper.ExecuteDataSet(sSql).Tables[0];
            dtInfo.PrimaryKey = new DataColumn[] { dtInfo.Columns["Role_ID"], dtInfo.Columns["Func_ID"] };
            dtInfo.TableName = "P_Auth_Role_Function";
            dsInfo.Tables.Add(dtInfo.Copy());
            return dsInfo;
        }
        internal DataTable GetAllRoleData()
        {
            string strSql = "SELECT * FROM P_Auth_Role ";
            return DBHelper.ExecuteDataSet(strSql).Tables[0];
        }
        #region add by 刘英男 检查用户角色名是否唯一
        /// <summary>
        ///  检查用户角色名是否唯一
        /// </summary>
        /// <param name="roleid">角色名</param>
        /// <returns>数量</returns>
        internal int CheckRoleName(string rolename)
        {
            string sql = "select Count(*) from p_auth_role where Role_Name='" + rolename + "'";
            int obj = (int)int.Parse(DBHelper.ExecuteScalar(sql).ToString());
            return obj;
        }
        #endregion
        #region add by 刘英男 检查用户主键是否唯一
        /// <summary>
        ///  检查用户主键是否唯一
        /// </summary>
        /// <param name="RoleCheckID">用户登录id</param>
        /// <returns>数量</returns>
        internal int CheckRolePrimary(string RoleCheckID)
        {
            string sql = "select Count(*) from p_auth_role where role_id='" + RoleCheckID + "'";
            int obj = (int)int.Parse(DBHelper.ExecuteScalar(sql).ToString());
            return obj;
        }
        #endregion
        internal void ModifyRoleInfo(string userName, DateTime userTime, string mainKey)
        {
            string sql = "update P_Auth_Role set CreateUser='" + userName + "',CreateTime='" + userTime + "'where Role_ID='" + mainKey + "' ";
            DBHelper.ExecuteSql(sql);
        }
        #region add by 刘英男 20111102 角色名称
        /// <summary>
        /// 根据主键获取角色名称
        /// </summary>
        /// <param name="pkValue">主键</param>
        /// <returns>pkName</returns>
        internal String GetpkNameByRolepkValueInfo(string pkValue)
        {
            string strSql = "select Role_Name from p_auth_role where Role_ID='" + pkValue + "'";
            return DBHelper.ExecuteScalar(strSql).ToString();
        }
        #endregion
    }
}
